package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbBannerMapper;
import com.zfei.gfb.device.AppContext;
import com.zfei.gfb.model.GfbBanner;
import com.zfei.gfb.model.GfbUser;
import com.zfei.gfb.query.GfbBannerQuery;
import com.zfei.gfb.request.GfbBannerAddRequest;
import com.zfei.gfb.request.GfbBannerEditRequest;
import com.zfei.gfb.request.GfbDelRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.vo.GfbBannerListVo;
import com.zfei.gfb.vo.GfbBannerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @version 1.0
 * @author
 * notes banner最多可发布五个，超过五个后无新增按钮
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "banner接口")
@RestController
@RequestMapping("/web/banner")
public class GfbBannerController  {

	@Resource
	private GfbBannerMapper gfbBannerMapper;

	@Resource
	private HttpSession httpSession;

	private static final int MAX_SIZE = 5;


	@ApiOperation(value = "列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbBannerListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list() {

		GfbBannerListVo result = new GfbBannerListVo();

		int total = gfbBannerMapper.totalAll();
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbBanner> records = gfbBannerMapper.listAll();
		List<GfbBannerVo> vos  = Beans.copys(records,GfbBannerVo.class);
		result.setData(vos);

		return result;
	}

	@ApiOperation(value = "新增")
	@LogResult
	@HttpJson("/add")
	public Object add(@RequestBody GfbBannerAddRequest request) {

		request.checkInsertNull();
		int i = gfbBannerMapper.totalAll();
		if(i >= MAX_SIZE){
			throw new StatusException(Status.ENOUGH);
		}
		GfbBanner record = Beans.copy(request, GfbBanner.class);

		GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);

		record.setUserId(user.getId());
		record.setCreateTime(Now.millis());
		record.setUpdateTime(Now.millis());
		record.setCntime(DateUtil.now());

		gfbBannerMapper.insertSelective(record);
		return null;
	}

	@ApiOperation(value = "编辑")
	@LogResult
	@HttpJson("/edit")
	public Object edit(@RequestBody GfbBannerEditRequest request) {

		request.checkUpdateNull();

		GfbBanner record = gfbBannerMapper.selectByPrimaryKey(request.getId());
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}
		BeanUtils.copyProperties(request, record);

		record.setUpdateTime(Now.millis());
		gfbBannerMapper.updateByPrimaryKeySelective(record);

		return null;
	}

	@ApiOperation(value = "删除")
	@LogResult
	@HttpJson("/del")
	public Object del(@RequestBody GfbDelRequest request) {
		request.checkDelNull();
		Integer id = request.getId();

		GfbBanner banner = gfbBannerMapper.selectByPrimaryKey(id);
		if (banner == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		gfbBannerMapper.deleteByPrimaryKey(id);
		return null;
	}

}
